package com.ruoyi.web.controller.wx;

import ch.qos.logback.core.util.StringCollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.controller.wx.model.WxConfig;
import com.ruoyi.web.controller.wx.util.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * \* @author: hasee
 * \* Date: 2023/5/30
 * \* Time: 19:40
 * \* Description:
 * \
 */
@RestController
@RequestMapping("/wx")
public class WxMpController {

    @Autowired
    private WxConfig wxConfig;
    @Autowired
    private RedisCache redisCache;

    @RequestMapping
    public JSONObject index() throws UnsupportedEncodingException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("wxConfig", wxConfig);
        jsonObject.put("redirectUri", wxConfig.getServer() + "/wx/toWxLogin");
        // 进行编码
        return jsonObject;
    }

    //获取openid和access_token的连接
    private static String getOpenId = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";
    //获取用户基本信息的连接
    private static String getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";

    /**
     * 微信网页授权回调
     *
     * @param code
     * @return
     */
    @RequestMapping("getCode")
    public R<JSONObject> getCode(String code, HttpServletResponse resp) throws IOException {
        String getOpenIdUrl = getOpenId.replace("APPID", wxConfig.getAppId()).replace("APPSECRET", wxConfig.getAppSecret()).replace("CODE", code);
        String response = HttpClientUtil.doGet(getOpenIdUrl);
        JSONObject jsonObject = JSONObject.parseObject(response);
        String accessToken = jsonObject.get("access_token") == null ? null : jsonObject.get("access_token").toString();
        String openId = jsonObject.get("openid") == null ? null : jsonObject.get("openid").toString();
        JSONObject result = new JSONObject();
        JSONObject userInfo = getUserInfo(openId, accessToken);

        result.put("accessToken", accessToken);
        result.put("openId", openId);
        result.put("userInfo",userInfo);
        return R.ok(result);
    }

    @RequestMapping("toWxLogin")
    public void toWxLogin(String code, HttpServletResponse resp) throws IOException {
        resp.sendRedirect("http://localhost:8081/#/pages/login/login?code=" + code);
    }

    /**
     * 获取微信用户信息
     *
     * @param openId
     * @param accessToken
     * @return
     */
    private JSONObject getUserInfo(String openId, String accessToken) {
        JSONObject jsonObject=new JSONObject();
        if(StringUtils.isEmpty(openId)||StringUtils.isEmpty(accessToken)){
            jsonObject.put("msg","参数错误");
        }
        String userInfoUrl = getUserInfo.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
        String userInfo = HttpClientUtil.doGet(userInfoUrl);
         jsonObject = JSONObject.parseObject(userInfo);
        return jsonObject;
    }

}